<?php

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\ProjectListModel;

class ProjectList extends BaseController
{
    public function query()
    {
        $params = $this->request->param();
        $list = ProjectListModel::where('village_id',$this->villageId)
            ->paginate([
                'list_rows' => $params['pageSize'],
                'page' => $params['pageNo']
            ]);
        return show($list);
    }

    public function add()
    {
        $params = $this->request->param();

        if(empty($params['name'])) {
            return show([],400,'缺少关键参数name');
        }
        if(empty($params['value'])) {
            return show([],400,'缺少关键参数value');
        }

        ProjectListModel::create([
            'name' => $params['name'],
            'value' => $params['value'],
            'village_id' => $this->villageId
        ]);

        return show();
    }

    public function edit()
    {
        $params = $this->request->param();
        if(empty($params['name'])) {
            return show([],400,'缺少关键参数name');
        }
        if(empty($params['value'])) {
            return show([],400,'缺少关键参数value');
        }

        if(empty($params['id'])) {
            return show([],400,'缺少关键参数id');
        }

        $projectListModel = ProjectListModel::find($params['id']);
        $projectListModel->name = $params['name'];
        $projectListModel->value = $params['value'];
        $projectListModel->save();

        return show();

    }

    public function del()
    {
        $params = $this->request->param();

        ProjectListModel::destroy($params['id']);

        return show();

    }
}